<?php
include("../common.inc.php");
include("function_common.php");
include_once(INC_PATH."/goodsforecast.class.php");
include_once(INC_PATH."/member.class.php");
function isPost() {
	return strtoupper($_SERVER['REQUEST_METHOD']) == 'POST' ? true : false;
}
function regex($value,$rule) {
	$validate = array(
			'double' => '/^\d+(\.\d+)?$/',
	);
	// 检查是否有内置的正则表达式
	if(isset($validate[strtolower($rule)]))
	{
		$rule   =   $validate[strtolower($rule)];
	}
	return preg_match($rule,$value)===1;
}
$g = new GoodsForecastClass();
InitGP(array('action'));
if($action == 'edit'){
	InitGP(array('gid','status','weight','price','depot', 'extype', 'ex', 'bag', 'remark', 'uid', 'shops', 'insure'));
	$dataarray = array();
	$dataarray["depot"] = GetNum($depot);
	$dataarray["extype"] = Char_cv($extype);
	$dataarray['shops'] = Char_cv($shops);
	$dataarray["ex"] = Char_cv($ex);
	$dataarray["bag"] = Char_cv($bag);
	$dataarray["remark"] = Char_cv($remark);
	$dataarray['addtime'] = time();
	$dataarray["insure"] = GetNum($insure);
	
	if($status == 3){
		if(regex($weight, 'double')){
			$dataarray['storagetime'] = time();
			$dataarray['weight'] = $weight;
			$dataarray['status'] = $status;
		}else{
			showmsg('重量正确填写', '-1');
		}
	}else{
		if($weight){
			if(regex($weight, 'double')){
				$dataarray['weight'] = $weight;
			}else{
				showmsg('重量正确填写', '-1');
			}
		}
	}
	$dataarray['checktime'] = time();
	$good = $g->getone($gid);
	$good['status_info'] = $g->getStatusInfo($good['status']);
	if(isPost()){
		if($g->edit($gid, $dataarray) == 'OK'){
			showmsg('修改成功！', 'goodsforecast.php?status='.$good['status']);
		}
	}
	include("tpl/goodsforecast_edit.htm");
}else if($action == 'doadd'){
	$m = new memberclass();
	InitGP(array('depot', 'extype', 'ex', 'bag', 'remark', 'status', 'uid', 'shops', 'weight', 'insure'));
	$addarray["depot"] = GetNum($depot);
	$addarray["extype"] = Char_cv($extype);
	$addarray['shops'] = Char_cv($shops);
	$addarray["ex"] = Char_cv($ex);
	$addarray["bag"] = Char_cv($bag);
	$addarray["remark"] = Char_cv($remark);
	$addarray["status"] = GetNum($status);
	$addarray["uid"] = GetNum($uid);
	$addarray['addtime'] = time();
	$addarray["insure"] = GetNum($insure);
	$addarray['storagetime'] = time();
	if(regex($weight, 'double')){
		$addarray['weight'] = $weight;
	}else{
		showmsg('重量正确填写', '-1');
	}
	//获取用户信息
	$user = $m->getone($addarray["uid"] );
	if($user){
		$addarray['uname'] = $user['uname'];
	}else{
		showmsg('你填写的用户不存在。', '-1');
	}
	if(!$addarray["depot"] || !$addarray["extype"] || !$addarray["ex"] || !$addarray["bag"] || !$addarray['shops']){
		showmsg('所有的信息必须填写。', '-1');
	}
	if($g->checkEx($addarray["extype"], $addarray["ex"])){
		showmsg('你添加的快递信息已经存在。', '-1');
	}
	if($g->add($addarray)){
		showmsg('成功添加！ ', 'goodsforecast.php?action=edit&gid='.$gid);
	}
	exit;
}else if($action == 'add'){
	include("tpl/goodsforecast_add.htm");
}else{
	InitGP(array('page', 'status', 'uname')); //初始化变量全局返回
	$Table=new TableClass("goodsforecast","gid");
	AjaxHead();//禁止页面缓存
	$pagesize = 15;
	$page = isset($page) ? max(1, intval($page)) : 1;             //处理页码变量
	$offset = ($page - 1) * $pagesize;        //偏移量
	$orderstr = 'gid DESC';
	$where = null;
	if(intval($status)){
		$where = ' status = '.$status;
	}

	if($uname){
		if($where){
			$where .= ' AND ';
		}
		$where .= ' uname like \'%'.$uname.'%\'';
	}
	$total = $Table->getcount($where);
	$pagesize = 15;
	$dataarray = $Table->getdata("$offset,$pagesize", $where, $orderstr);
	foreach ($dataarray as $key=>$ex){
		$dataarray[$key]['status_info'] = $g->getStatusInfo($ex['status']);
	}
	include("tpl/goodsforecast.htm");
}
?>